/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.nxu.campus.zhxy.service.impl;

import com.nxu.campus.utils.FileUtil;
import com.nxu.campus.utils.PageUtil;
import com.nxu.campus.utils.QueryHelp;
import com.nxu.campus.utils.ValidationUtil;
import com.nxu.campus.zhxy.domain.ZhxyClasse;
import com.nxu.campus.zhxy.repository.ZhxyClasseRepository;
import com.nxu.campus.zhxy.service.ZhxyClasseService;
import com.nxu.campus.zhxy.service.dto.ZhxyClasseDto;
import com.nxu.campus.zhxy.service.dto.ZhxyClasseQueryCriteria;
import com.nxu.campus.zhxy.service.mapstruct.ZhxyClasseMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author HWL
* @date 2022-04-18
**/
@Service
@RequiredArgsConstructor
public class ZhxyClasseServiceImpl implements ZhxyClasseService {

    private final ZhxyClasseRepository zhxyClasseRepository;
    private final ZhxyClasseMapper zhxyClasseMapper;

    @Override
    public Map<String,Object> queryAll(ZhxyClasseQueryCriteria criteria, Pageable pageable){
        Page<ZhxyClasse> page = zhxyClasseRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(zhxyClasseMapper::toDto));
    }

    @Override
    public List<ZhxyClasseDto> queryAll(ZhxyClasseQueryCriteria criteria){
        return zhxyClasseMapper.toDto(zhxyClasseRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public ZhxyClasseDto findById(Long classeId) {
        ZhxyClasse zhxyClasse = zhxyClasseRepository.findById(classeId).orElseGet(ZhxyClasse::new);
        ValidationUtil.isNull(zhxyClasse.getId(),"ZhxyClasse","classeId",classeId);
        return zhxyClasseMapper.toDto(zhxyClasse);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ZhxyClasseDto create(ZhxyClasse resources) {
        return zhxyClasseMapper.toDto(zhxyClasseRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ZhxyClasse resources) {
        ZhxyClasse zhxyClasse = zhxyClasseRepository.findById(resources.getId()).orElseGet(ZhxyClasse::new);
        ValidationUtil.isNull( zhxyClasse.getId(),"ZhxyClasse","id",resources.getId());
        zhxyClasse.copy(resources);
        zhxyClasseRepository.save(zhxyClasse);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long classeId : ids) {
            zhxyClasseRepository.deleteById(classeId);
        }
    }

    @Override
    public void download(List<ZhxyClasseDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ZhxyClasseDto zhxyClasse : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("年级", zhxyClasse.getGrade());
            map.put("班级代码", zhxyClasse.getClasseCode());
            map.put("班级名称", zhxyClasse.getName());
            map.put("专业", zhxyClasse.getMajor().getName());
            map.put("建班日期", zhxyClasse.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
